package net.easyjava.tools.model;

import net.easyjava.tools.BaseBean;
import net.easyjava.tools.json.filter.FilterProperty;
import net.easyjava.tools.page.Page;
import net.sf.json.JSONArray;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Created by wxnacy on 15/12/17.
 */
public class ModelBean extends BaseBean {
    private static final long serialVersionUID = -5754814579884800211L;
    private int status;//1:成功/0:失败
    private String message;//接口信息
    private long timestamp;//时间戳
    private Object data;//返回数据

    public ModelBean() {
        this.status = ModelConstants.STATUS_SUCCESS;
        this.message =ModelConstants.MESSAGE_SUCCESS;
        this.timestamp = System.currentTimeMillis();
    }

    public ModelBean(String message) {
        this.status = ModelConstants.STATUS_ERROR;
        this.message = message;
        this.timestamp = System.currentTimeMillis();
    }

    public ModelBean(int status, String message) {

        this();
        this.status = status;
        this.message = message;
    }

    public ModelBean(Object data) {
        this();
        this.data = data;
    }
    public ModelBean(Object data,String filter) {
        this(ModelConstants.STATUS_SUCCESS, ModelConstants.MESSAGE_SUCCESS);
        if (filter != null && !filter.equals("")) {
            if(data instanceof Page){
                this.data = useArrayFilter(((Page) data).getRows(),filter);
            }else{
                this.data = useFilter(data, filter);
            }
        } else {
            this.data = data;
        }
    }
    private Object useFilter(Object object, String filter) {
        FilterProperty fp = new FilterProperty();
        return fp.configJsonObject(object, filter);
    }

    private JSONArray useArrayFilter(Collection<Object> rows, String filter) {
        FilterProperty fp = new FilterProperty();
        return fp.configJsonArray(rows, filter);
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }


    public static void main(String[] args) {
        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        jsonObject.put("name",new Date());
        jsonObject.put("id", "www");
        List list = new ArrayList();
        list.add(jsonObject);
        Page page = new Page(list,list.size(),1);
        ModelBean modelBean = new ModelBean(page,"name");
        System.out.println("modelBean = " + modelBean);
    }
}
